﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportServiceRef;
    using WMS6.Web.UserControls;

    public class ShipmentStatByCustomerRptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F15471;
        protected GridView gvShipmentStat;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected Label lblMsg;
        protected Label lblShipmentStatByCustomerRptList;
        protected Label lblTotalCube;
        protected Label lblTotalCubeText;
        protected Label lblTotalGross;
        protected Label lblTotalGrossText;
        protected Label lblTotalPrice;
        protected Label lblTotalPriceText;
        protected Label lblTotalShippedQty;
        protected Label lblTotalShippedQtyText;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        private decimal TotalCube;
        private decimal TotalGross;
        private decimal TotalOriginalQty;
        private decimal TotalPrice;
        private decimal TotalShippedQty;
        protected UpdatePanel up1;

        public void BindData2GridView(ShipmentStatByCustomerRptQueryEntity shipmentStatByCustomerRptQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(shipmentStatByCustomerRptQueryEntity.CurrentPage);
            DataSet ds = AnalyticsReportServiceClientProxy.GetInstance().GetShipmentStatByCustomerRptDataSet(shipmentStatByCustomerRptQueryEntity);
            base.BindData2GridView(this.gvShipmentStat, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            ShipmentStatByCustomerRptQueryEntity shipmentStatByCustomerRptQueryEntity = this.GetShipmentStatByCustomerRptQueryEntity();
            shipmentStatByCustomerRptQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = shipmentStatByCustomerRptQueryEntity.CurrentPage;
            shipmentStatByCustomerRptQueryEntity.CurrentPage = "0";
            try
            {
                using (DataSet ds = AnalyticsReportServiceClientProxy.GetInstance().GetShipmentStatByCustomerRptDataSet(shipmentStatByCustomerRptQueryEntity))
                {
                    base.Export2Excel("ShipmentStatByCustomer.xls", "15471", this.gvShipmentStat, ds);
                }
            }
            finally
            {
                shipmentStatByCustomerRptQueryEntity.IsGetAll = false;
                shipmentStatByCustomerRptQueryEntity.CurrentPage = currentPageIndex;
                this.SetShipmentStatByCustomerRptQueryEntity(shipmentStatByCustomerRptQueryEntity);
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            ShipmentStatByCustomerRptQueryEntity shipmentStatByCustomerRptQueryEntity = this.GetShipmentStatByCustomerRptQueryEntity();
            shipmentStatByCustomerRptQueryEntity.CurrentPage = "0";
            this.BindData2GridView(shipmentStatByCustomerRptQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            ShipmentStatByCustomerRptQueryEntity shipmentStatByCustomerRptQueryEntity = this.GetShipmentStatByCustomerRptQueryEntity();
            shipmentStatByCustomerRptQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2GridView(shipmentStatByCustomerRptQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            ShipmentStatByCustomerRptQueryEntity shipmentStatByCustomerRptQueryEntity = this.GetShipmentStatByCustomerRptQueryEntity();
            shipmentStatByCustomerRptQueryEntity.OWNER_ID = "";
            shipmentStatByCustomerRptQueryEntity.StartDateTime = "";
            shipmentStatByCustomerRptQueryEntity.EndDateTime = "";
            this.BindData2GridView(shipmentStatByCustomerRptQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            ShipmentStatByCustomerRptQueryEntity shipmentStatByCustomerRptQueryEntity = this.GetShipmentStatByCustomerRptQueryEntity();
            if (int.Parse(shipmentStatByCustomerRptQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                shipmentStatByCustomerRptQueryEntity.CurrentPage = (int.Parse(shipmentStatByCustomerRptQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2GridView(shipmentStatByCustomerRptQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            ShipmentStatByCustomerRptQueryEntity shipmentStatByCustomerRptQueryEntity = this.GetShipmentStatByCustomerRptQueryEntity();
            if (int.Parse(shipmentStatByCustomerRptQueryEntity.CurrentPage) > 0)
            {
                shipmentStatByCustomerRptQueryEntity.CurrentPage = (int.Parse(shipmentStatByCustomerRptQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2GridView(shipmentStatByCustomerRptQueryEntity);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            ShipmentStatByCustomerRptQueryEntity shipmentStatByCustomerRptQueryEntity = this.GetShipmentStatByCustomerRptQueryEntity();
            this.BindData2GridView(shipmentStatByCustomerRptQueryEntity);
        }

        private ShipmentStatByCustomerRptQueryEntity GetShipmentStatByCustomerRptQueryEntity()
        {
            return (base.PageQueryEntitySession as ShipmentStatByCustomerRptQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                ShipmentStatByCustomerRptQueryEntity shipmentStatByCustomerRptQueryEntity = this.GetShipmentStatByCustomerRptQueryEntity();
                shipmentStatByCustomerRptQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2GridView(shipmentStatByCustomerRptQueryEntity);
            }
        }

        protected void gvShipmentStat_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15471", this.gvShipmentStat);
        }

        protected void gvShipmentStat_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                if (((Label) e.Row.FindControl("lblQTY")) != null)
                {
                    this.TotalShippedQty += decimal.Parse((((Label) e.Row.FindControl("lblQTY")).Text != "") ? ((Label) e.Row.FindControl("lblQTY")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblGROSS_WGT")) != null)
                {
                    this.TotalGross += decimal.Parse((((Label) e.Row.FindControl("lblGROSS_WGT")).Text != "") ? ((Label) e.Row.FindControl("lblGROSS_WGT")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblCUBE")) != null)
                {
                    this.TotalCube += decimal.Parse((((Label) e.Row.FindControl("lblCUBE")).Text != "") ? ((Label) e.Row.FindControl("lblCUBE")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblPRICE")) != null)
                {
                    this.TotalPrice += decimal.Parse((((Label) e.Row.FindControl("lblPRICE")).Text != "") ? ((Label) e.Row.FindControl("lblPRICE")).Text : "0");
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblTotalShippedQtyText.Text = base.GetPreferenceDigit(this.TotalShippedQty);
                this.lblTotalGrossText.Text = base.GetPreferenceDigit(this.TotalGross);
                this.lblTotalCubeText.Text = base.GetPreferenceDigit(this.TotalCube);
                this.lblTotalPriceText.Text = base.GetPreferenceDigit(this.TotalPrice);
            }
        }

        protected void gvShipmentStat_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                ShipmentStatByCustomerRptQueryEntity shipmentStatByCustomerRptQueryEntity = this.GetShipmentStatByCustomerRptQueryEntity();
                shipmentStatByCustomerRptQueryEntity.SortField = e.SortExpression;
                if (shipmentStatByCustomerRptQueryEntity.SortDirection == "ASC")
                {
                    shipmentStatByCustomerRptQueryEntity.SortDirection = "DESC";
                }
                else if (shipmentStatByCustomerRptQueryEntity.SortDirection == "DESC")
                {
                    shipmentStatByCustomerRptQueryEntity.SortDirection = "ASC";
                }
                this.BindData2GridView(shipmentStatByCustomerRptQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            ShipmentStatByCustomerRptQueryEntity shipmentStatByCustomerRptQueryEntity = new ShipmentStatByCustomerRptQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    shipmentStatByCustomerRptQueryEntity.OWNER_ID = this.PreviousPage.OwnerID;
                    shipmentStatByCustomerRptQueryEntity.StartDateTime = this.PreviousPage.StartTime;
                    shipmentStatByCustomerRptQueryEntity.EndDateTime = this.PreviousPage.EndTime;
                }
                else
                {
                    shipmentStatByCustomerRptQueryEntity.OWNER_ID = "";
                    shipmentStatByCustomerRptQueryEntity.StartDateTime = "";
                    shipmentStatByCustomerRptQueryEntity.EndDateTime = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            shipmentStatByCustomerRptQueryEntity.IsGetAll = false;
            shipmentStatByCustomerRptQueryEntity.CurrentPage = "0";
            shipmentStatByCustomerRptQueryEntity.PageSize = this.gvShipmentStat.PageSize.ToString();
            shipmentStatByCustomerRptQueryEntity.SortField = "CUSTOMER_ID";
            shipmentStatByCustomerRptQueryEntity.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvShipmentStat.PageSize;
            this.SetShipmentStatByCustomerRptQueryEntity(shipmentStatByCustomerRptQueryEntity);
            this.BindData2GridView(shipmentStatByCustomerRptQueryEntity);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvShipmentStat);
                this.LoadData();
            }
        }

        public void SetShipmentStatByCustomerRptQueryEntity(ShipmentStatByCustomerRptQueryEntity shipmentStatByCustomerRptQueryEntity)
        {
            base.PageQueryEntitySession = shipmentStatByCustomerRptQueryEntity;
        }

        public ShipmentStatByCustomerRptQuery PreviousPage
        {
            get
            {
                return (ShipmentStatByCustomerRptQuery) base.PreviousPage;
            }
        }
    }
}

